package com.fang.mmy.items.filter;

import com.alibaba.fastjson.JSONArray;
import com.fang.mmy.entity.User;
import com.fang.mmy.items.constants.Constants;
import com.fang.mmy.items.entity.Item;
import com.fang.mmy.items.entity.ItemRuleDetail;
import com.fang.mmy.items.mapper.ItemRuleDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName RoleFilter
 * @Author kebukeyi
 * @Date 2022/8/9 18:34
 * @Description
 * @Version 1.0.0
 */
@Component
public class RoleFilter implements LogicFilter {


    @Autowired
    private ItemRuleDetailMapper itemRuleDetailMapper;


    @Override
    public boolean filter(User user, Item item) {
        // ["1","2","3","4"]
        String roles = user.getRoles();
        JSONArray parseArray = JSONArray.parseArray(roles);
        List<ItemRuleDetail> accountRuleByItemId = itemRuleDetailMapper.getAllAccountRuleByItemId(item.getId(), Constants.ItemRuleCode.Condition,Constants.ItemRuleDesc.Role);
        for (ItemRuleDetail ruleDetail : accountRuleByItemId) {
            String ruleValue = ruleDetail.getItemRuleValue();
            boolean contains = parseArray.contains(ruleValue);
            if (contains) return true;
        }
        return false;
    }


}
